<?php

class Public_Comment_DeleteAction extends BlogPublicBaseAction
{
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        $comment_id = $rd->getParameter('comment_id');
        /* @var $comments_model Public_CommentsModel */
        $comments_model = $this->getContext()->getModel('Comments', 'Public');
        $comment = $comments_model->getComment($comment_id);

        if (!$comment instanceof Comment) {
            return array('Public', 'Error404Success');
        }
        
        $this->setAttribute('comment', $comment);
        
        return 'Confirm';
    }
    
    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        $comment_id = $rd->getParameter('comment_id');
        /* @var $comments_model Public_CommentsModel */
        $comments_model = $this->getContext()->getModel('Comments', 'Public');
		/* @var $posts_model Public_PostsModel */
        $posts_model = $this->getContext()->getModel('Posts', 'Public');
        $comment = $comments_model->getComment($comment_id);
        
        if (!$comment instanceof Comment) {
            return array('Public', 'Error404Success');
        }
        
        $comments_model->deleteComment($comment_id);
        $this->setAttribute('comment', $comment);
        
        // clear cache
        $post = $comment->Post;
        $posts_model->clearPostCache($post);

        return 'Success';
    }
    
    public function isSecure()
    {
        return TRUE;
    }
    
    public function getCredentials()
    {
        return array('comments.moderator');
    }
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Success';
	}
}

?>
